$(function () {
    obj.init();
});

window.obj = new function() {
    'use strict';
    return {
        dataGrid: null,
        params: {
            schoolId: null,
            targetId: null
        },
        dom: {
            save: null
        },
        init: function () {
            this.params.schoolId = xhTool.getQuery('schoolId');
            this.params.targetId = xhTool.getQuery('targetId');

            this.dom.save = window.top.xhTop.getModalConfirmDom();

            this.dataGrid = new xhDataGrid($('#list'), 'School/getUnbindStud', {schoolId: this.params.schoolId});
            this.dataGrid.parseToolbar().init().activeBtn(this.dom.save, null, 'bs');
            xhExtra.clearLoadSuccess(this.dataGrid);

            this.listener();
        },
        listener: function () {
            var $this = this;
            xhExtra.clearLoadSuccess($this.dataGrid);
            //绑定搜索事件
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function(){
                    $this.funObj().search();
                })
            );
            //保存
            $this.dom.save.click(function() {
                $this.funObj().save();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                search: function () {
                    $this.dataGrid.reload({search: {vague: xhExtra.getValue($('#list_vague'))}});
                },
                save: function () {
                    var userIds = $this.dataGrid.getSelectValues('iUserId');
                    xhExtra.ajaxRequest(
                        'school/bindStudTablet',
                        {userIds:userIds, targetId: $this.params.targetId},
                        {success: true, check: false, table: window.top.xhTop.getActiveFrame().obj.dataGrid},
                        function (result) {
                            xhExtra.setBtnNormal($this.dom.save);
                            if (xhExtra.checkError(result)) return false;
                            window.top.xhTop.closeModal();
                        }
                    );
                }
            }
        }
    }
};